<template>
  <div class="login-container">
    <el-card class="login-card">
      <div class="login-title">后台管理系统登录</div>
      <el-form :model="form" :rules="rules" ref="formRef" label-width="0">
        <el-form-item prop="username">
          <el-input v-model="form.username" placeholder="用户名" prefix-icon="el-icon-user" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" type="password" placeholder="密码" prefix-icon="el-icon-lock" show-password />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width:100%;" @click="onLogin" :loading="loading">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = useRouter()
const formRef = ref()
const form = ref({
  username: '',
  password: ''
})
const loading = ref(false)
const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
}

function onLogin() {
  formRef.value.validate(async (valid) => {
    if (!valid) return
    loading.value = true
    // 简单模拟登录，实际应调用后端API
    setTimeout(() => {
      loading.value = false
      if (form.value.username === 'admin' && form.value.password === 'admin') {
        localStorage.setItem('isLogin', '1')
        ElMessage.success('登录成功')
        router.replace('/dashboard')
      } else {
        ElMessage.error('用户名或密码错误')
      }
    }, 800)
  })
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f0f6ff;
}
.login-card {
  width: 340px;
  padding: 32px 24px 18px 24px;
  border-radius: 10px;
  box-shadow: 0 2px 16px #e3eaf7;
}
.login-title {
  text-align: center;
  font-size: 22px;
  font-weight: bold;
  margin-bottom: 28px;
  color: #409eff;
}
</style>
